package com.shinbada.modules.sys.company;

import com.google.common.collect.Lists;
import com.shinbada.common.utils.IdGen;
import com.shinbada.modules.sys.entity.Office;
import com.shinbada.modules.sys.entity.Role;
import com.shinbada.modules.sys.entity.SysSetting;
import com.shinbada.modules.sys.entity.User;
import com.shinbada.modules.sys.service.UserService;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;

/**
 * 机构注册
 *
 * @author initcap
 * @date Created in 2021/3/9 15:44.
 */
@Data
@ApiModel("机构注册")
public class CompanyRegister {

    @ApiModelProperty("公司名称[必填]")
    @NotBlank(message = "公司名称必填!")
    private String companyName;
    @ApiModelProperty("公司编码")
    private String companyCode;

    @ApiModelProperty("用户名[必填]")
    @NotBlank(message = "用户名称必填!")
    private String username;
    @ApiModelProperty("登录名[必填]")
    @NotBlank(message = "用户登录名称必填!")
    private String loginName;
    @ApiModelProperty("密码[必填]")
    @NotBlank(message = "用户登录密码必填!")
    private String password;
    @ApiModelProperty("卡号[必填]")
    @NotBlank(message = "用户卡号必填!")
    private String no;

    @ApiModelProperty("菜单主键集合，用逗号分割, [必填]")
    @NotBlank(message = "必须给管理员指定其拥有的权限!")
    private String menuIds;

    @ApiModelProperty("默认主题颜色, [必填]")
    @NotBlank(message = "必须指定默认主题颜色!")
    private String defaultTheme;
    @ApiModelProperty("默认主题风格, [必填]")
    @NotBlank(message = "必须指定默认主题风格!")
    private String defaultLayout;
    @ApiModelProperty("缩进LOGO, [必填]")
    @NotBlank(message = "必须设置缩进LOGO图标!")
    private String logo;
    @ApiModelProperty("产品LOGO, [必填]")
    @NotBlank(message = "必须设置LOGO图标!")
    private String logoLong;
    @ApiModelProperty("默认主题风格, [必填]")
    @NotBlank(message = "必须指定默认主题风格!")
    private String defaultThemeStyle;
    @ApiModelProperty("是否打开多页签功能, [必填]")
    @NotBlank(message = "必须指定是否打开多页签功能!")
    private String openTab;

    @ApiModelProperty("复制字典的目标机构, [必填]")
    @NotBlank(message = "必须指定字典目标机构!")
    private String sourceDictCompanyId;

    public Office toOffice() {
        Office office = new Office();
        office.setId("");
        office.setDelFlag(Office.DEL_FLAG_NORMAL);
        office.setName(this.companyName);
        office.setCode(this.companyCode);
        office.setUseable("1");
        office.setType("1");
        office.setAddress("");
        office.setMaster("");
        office.setPhone("");
        office.setFax("");
        office.setEmail("");
        return office;
    }

    public User toUser(String companyId, String roleId) {
        User user = new User();
        user.setId("");
        user.setDelFlag(User.DEL_FLAG_NORMAL);
        user.setName(this.username);
        user.setLoginName(this.loginName);
        user.setLoginFlag("1");
        user.setNo(this.no);
        user.setPassword(UserService.entryptPassword(this.password));
        user.setCompany(new Office(companyId));
        user.setOffice(new Office(companyId));
        user.setRoleList(Lists.newArrayList(new Role(roleId)));
        return user;
    }

    public Role toRole(String companyId) {
        Role role = new Role("");
        role.setDelFlag(Role.DEL_FLAG_NORMAL);
        role.setName("管理员");
        role.setSysData("1");
        role.setUseable("1");
        role.preInsert();
        role.setCompanyId(companyId);

        // 所有菜单权限
        role.setMenuIds(this.menuIds);
        return role;
    }

    public SysSetting toSetting(String companyId) {
        SysSetting setting = new SysSetting("");
        setting.setCompanyId(companyId);
        setting.setId(IdGen.uuid());
        setting.setDefaultTheme(this.defaultTheme);
        setting.setDefaultLayout(this.defaultLayout);
        setting.setLogo(this.logo);
        setting.setLogoLong(this.logoLong);
        setting.setDefaultThemeStyle(this.defaultThemeStyle);
        setting.setOpenTab(this.openTab);
        return setting;
    }
}
